package com.tedu.api.io;

import org.apache.commons.lang3.ObjectUtils;
import org.junit.jupiter.api.Test;

import java.io.*;


/**
 * 作业⭐    读取IOS API.java 文件到控制台
 *         提示：按照从上到下执行逻辑---标准流：IO+异常捕获
 * 作业⭐⭐  写出代码段5个错误【分开：1次抓一个错误】。使用try--catch捕获打印信息
 *         提示：字符串，集合，标准IO，包装类--看API
 */
public class HomeWork2 {

    @Test  //作业
    public void testHomeWork1(){
        //1.创建一个低级流---针对  读操作【找到ISOAPI.java】
        FileInputStream fis = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        //maven项目---src\main\java\com\tedu\api\io\ISOAPI.java
        //2.创建转换流----指定编码集：UTF8  【不能直接输出字符串】

        try {
            fis = new FileInputStream("src" + File.separator + "main" + File.separator + "java" + File.separator +
                    "com" + File.separator + "tedu" + File.separator + "api" + File.separator +
                    "io" + File.separator + "IOSAPI.java");
            isr = new InputStreamReader(fis, "UTF8");
            //3.【能直接输出字符串】创建缓冲数据流
            br = new BufferedReader(isr);
            //4.创建保存每行字符串的变量
            String line = null;
            //5.一直  读取字符串，不等于null，又字符串--同时写出
            while((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(UnsupportedEncodingException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br!=null)
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    @Test //①
    public void testException21() {
        FileInputStream diyigeyichang = null;
        try {
            diyigeyichang = new FileInputStream("111.java");
        } catch (FileNotFoundException e) {      //第一个异常
            e.printStackTrace();
        } finally {
        }
    }

    @Test  //②
    public void testException22() {
        try {
            String s = "diergeyichang";
            s.length();
            Integer.parseInt(s);
        } catch (NumberFormatException e) {      //第二个异常
            e.printStackTrace();
        } finally {
        }
    }
    @Test  //③
    public void testException23() throws FileNotFoundException {
        FileInputStream diyigeyichang=null;
        InputStreamReader disangeyichang=null;
        diyigeyichang=new FileInputStream("ISO.java");
        try {
            disangeyichang=new InputStreamReader(diyigeyichang,"UTF8");
        } catch (UnsupportedEncodingException e) {   //第三个异常
            e.printStackTrace();
        } finally {
        }
    }
    @Test  //④
    public void testException24(){
        BufferedReader br=null;
        br  = new BufferedReader(null);
        String line=null;
        try {
            while((line=br.readLine())!=null);
        } catch (IOException e) {       //第四个异常
            e.printStackTrace();
        } finally {
        } {
            System.out.println(line);
        }
    }

    public static void main(String[] args) {
        String wuwuwu = null;
        try {
            int length = wuwuwu.length();
            System.out.println(length);
        } catch (NullPointerException e) {//第五个异常
            e.printStackTrace();
        } finally {
        }
    }

}


